package com.DT.netTool;

import android.util.Log;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UDPThread extends Thread {
	private String target_ip = "";
	private boolean isRun;

	public boolean isRun(){
		return isRun;
	}
	
	public static final byte[] NBREQ = { (byte) 0x82, (byte) 0x28, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1,
		(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x20, (byte) 0x43, (byte) 0x4B,
		(byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41,
		(byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41,
		(byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x41,
		(byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x0, (byte) 0x0, (byte) 0x21, (byte) 0x0, (byte) 0x1 };
	
	public static final short NBUDPP = 137;

	public UDPThread(String target_ip) {
		this.target_ip = target_ip;
	}

	@Override
	public synchronized void run() {
		 isRun=true;
		Log.w("run","start");
		if (target_ip == null || target_ip.equals("")) return;
		DatagramSocket socket = null;
		InetAddress address = null;
		DatagramPacket packet = null; //单播
		try {
			address = InetAddress.getByName(target_ip);
			packet = new DatagramPacket(NBREQ, NBREQ.length, address, NBUDPP);
			socket = new DatagramSocket();
			socket.setSoTimeout(200);
			socket.send(packet);
			socket.close();
		} catch (SocketException se) {
		} catch (UnknownHostException e) {
		} catch (IOException e) {
		} finally {
			if (socket != null) {
				socket.close();
			}
		}
		isRun=false;
		Log.w("run","stop");
	}

	public static String getItName(){
		String name="";
		String ip="";
		try {
			// 获取计算机名
			name = InetAddress.getLocalHost().getHostName();
			// 获取IP地址
			ip = InetAddress.getLocalHost().getHostAddress();
			System.out.println("计算机名："+name);
			System.out.println("IP地址："+ip);
		} catch (UnknownHostException e) {
			System.out.println("异常：" + e);
			e.printStackTrace();
		}
		return name+" "+ip;
	}
}
